package com.lagou.goods.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.common.pojo.Goods;
import com.lagou.common.pojo.GoodsVo;
import com.lagou.common.pojo.ResponseResult;
import com.lagou.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @BelongProject: lagou-parent
 * @author: 胡恺恺
 * @createTime: 2021/1/29  16:31
 * @Description:
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    /**
     * restful格式进行访问
     * 查询：GET
     * 新增: POST
     * 更新：PUT
     * 删除: DELETE
     */

    @Autowired
    private GoodsService goodsService;

     /*
        用户分页&多条件组合查询方法
     */

    @GetMapping("/queryByPage")
    public ResponseResult queryByPage(GoodsVo goodsVo){

        PageInfo pageInfo = goodsService.queryByPage(goodsVo);

        //封装响应数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("list", pageInfo.getList());

        return new ResponseResult(true,200,"分页多条件查询成功",map);

    }

    /**
     * 通过id查询商品
     */
    @GetMapping("/queryById/{id}")
    public ResponseResult queryById(@PathVariable("id") Integer id){

        Goods goods = goodsService.queryById(id);

        return new ResponseResult(true,200,"查询商品成功",goods);

    }

    /**
     * 通过id删除商品
     */
    @DeleteMapping("/deleteById/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id){

        int i = goodsService.deleteById(id);

        return new ResponseResult(true,200,"删除商品成功",null);

    }

    /**
     * 通过id编辑商品
     */
    @PutMapping("/updateById")
    public ResponseResult deleteById(Goods goods){

        System.out.println(goods);

        int i = goodsService.updateById(goods);

        return new ResponseResult(true,200,"编辑商品成功",null);

    }
}
